package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jaya.budan.R;
import com.jaya.budan.bean.User_Limit;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.views.Circle_ImageView;

import java.util.ArrayList;

public class Chat_ListView_Adapter extends BaseAdapter {

    private Context mcontext;
    private ArrayList<User_Limit> mlist;
    public Chat_ListView_Adapter(Context context, ArrayList<User_Limit> list){
        mcontext = context;
        mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"SetTextI18n", "ViewHolder"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(mcontext, R.layout.chat_listview_item, null);
        Circle_ImageView iv_nearby_item_icon = convertView.findViewById(R.id.iv_nearby_item_icon);
        ImageView iv_chat_item_online = convertView.findViewById(R.id.iv_chat_item_online);
        Glide.with(mcontext).load(mlist.get(position).getHead()).apply(new RequestOptions()
                .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
        ).into(iv_nearby_item_icon);
        TextView tv_chat_lv_item_last_msg = convertView.findViewById(R.id.tv_chat_lv_item_last_msg);
        TextView tv_chat_item_weidu = convertView.findViewById(R.id.tv_chat_item_weidu);
        TextView tv_nearby_listview_item_name = convertView.findViewById(R.id.tv_nearby_listview_item_name);
        TextView tv_nearby_listview_item_online = convertView.findViewById(R.id.tv_nearby_listview_item_online);
        ImageView iv_nearby_item_isvip = convertView.findViewById(R.id.iv_nearby_item_isvip);
        tv_nearby_listview_item_name.setText("" + mlist.get(position).getNicename());
        if (mlist.get(position).getIs_online() == 1){
            iv_chat_item_online.setVisibility(View.GONE);//离线
        } else {
            iv_chat_item_online.setVisibility(View.VISIBLE);//在线
        }
        if (mlist.get(position).getV2TIMConversation().getUnreadCount() > 0){
            tv_chat_item_weidu.setVisibility(View.VISIBLE);
        } else {
            tv_chat_item_weidu.setVisibility(View.GONE);
        }
        String date = ToolUtils.timeStamp2Date(mlist.get(position).getV2TIMConversation().getLastMessage().getTimestamp() + "", "yyyy-MM-dd HH:mm:ss");
        tv_chat_item_weidu.setText("" + mlist.get(position).getV2TIMConversation().getUnreadCount());//未读消息数
        tv_nearby_listview_item_online.setText("" + date);
        if (mlist.get(position).getV2TIMConversation().getLastMessage().getTextElem() != null){
            tv_chat_lv_item_last_msg.setText(mlist.get(position).getV2TIMConversation().getLastMessage().getTextElem().getText());
        }
        if (mlist.get(position).getV2TIMConversation().getLastMessage().getSoundElem() != null){
            tv_chat_lv_item_last_msg.setText("[语音]");
        }
        if (mlist.get(position).getV2TIMConversation().getLastMessage().getImageElem() != null){
            tv_chat_lv_item_last_msg.setText("[图片]");
        }
        if (mlist.get(position).getV2TIMConversation().getLastMessage().getLocationElem() != null){
            tv_chat_lv_item_last_msg.setText("[位置]");
        }
        if (mlist.get(position).getIs_vip() == 1){
            iv_nearby_item_isvip.setVisibility(View.VISIBLE);//是 会员
            tv_nearby_listview_item_name.setTextColor(Color.parseColor("#FF3B30"));
        } else {
            iv_nearby_item_isvip.setVisibility(View.INVISIBLE);
            tv_nearby_listview_item_name.setTextColor(Color.parseColor("#000000"));
        }
        return convertView;
    }
}
